package com.zygh.dlqwservice.common.domain;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lee
 */
public class ResultUtils {

    /**
     * 成功操作返回的resultbeans的格式
     * @param obj 对象
     * @return ResultBeans
     */
    public static ResultBeans resultBeansSuccess(Object obj) {
        ResultBeans resultBeans = new ResultBeans();
        resultBeans.setCode("200");
        resultBeans.setMessage("ok");
        Map<String,List> map = new HashMap<>(5);
        FieldValues fieldValues = new FieldValues();
        List<FieldValues> list = new ArrayList<>();
        fieldValues.setField("json");
        fieldValues.setValue(JSON.toJSONString(obj));
        list.add(fieldValues);
        map.put("fieldValues", list);
        resultBeans.getData().getDataList().add(map);
        return resultBeans;
    }

    /**
     * 错误操作返回的json格式
     * @param obj 对象参数
     * @return  ResultBeans
     */
    public static ResultBeans resultBeansError(Object obj) {
        ResultBeans resultBeans = new ResultBeans();
        resultBeans.setCode("500");
        resultBeans.setMessage("error");
        Map<String,List> map = new HashMap<>(5);
        List<FieldValues> list = new ArrayList<>();
        FieldValues fieldValues = new FieldValues();
        fieldValues.setField("json");
        fieldValues.setValue(JSON.toJSONString(obj));
        list.add(fieldValues);
        map.put("fieldValues", list);
        resultBeans.getData().getDataList().add(map);
        return resultBeans;
    }

    /**
     * 返回格式
     * @param count 数量
     * @param msg 消息
     * @return ResultBeans
     */
    public static ResultBeans resultBeansexecOp(int count, String msg) {
        if (count > 0) {
            return resultBeansSuccess(msg + "成功");
        } else {
            return resultBeansError(msg + "失败");
        }
    }

    /**
     * 返回结果
     * @param resultBean ResultBeans
     * @param content 上下文
     * @return ResultBeans
     */
    public static ResultBeans resultBeans(ResultBeans resultBean, String content) {
        try {
            JSONObject jsonObject = JSONObject.parseObject(content);
            resultBean.setMessageId(jsonObject.getString("messageId"));
            System.out.println("---------------------------------->messageId" + jsonObject.getString("messageId") + "---------------------------------->");
            resultBean.setVersion(jsonObject.getString("version"));
            resultBean.getData().getPage().setPageSize(jsonObject.getJSONObject("parameter").getJSONObject("page").getInteger("pageSize"));
            resultBean.getData().getPage().setPageNo(jsonObject.getJSONObject("parameter").getJSONObject("page").getInteger("pageNo"));
        } catch (Exception e) {
            System.out.println("请求体为空");
        }
        return resultBean;
    }
}
